package com.college.products.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.common.pojo.ao.Products;
import com.college.common.pojo.dto.products.AddProductsDTO;
import com.college.common.pojo.dto.products.QueryProductsDTO;
import com.college.common.pojo.dto.products.UpdateProductsDTO;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.products.mapper.ProductsMapper;
import com.college.products.service.ProductsService;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品 服务实现类
 * @author 小C工作室
 * @since 2025-02-22
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {
    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public List<Products> queryList(QueryProductsDTO queryProductsDTO) {
        return productsMapper.queryList(queryProductsDTO);
    }

    @Override
    public List<Products> queryListPage(QueryProductsDTO queryProductsDTO, PageParamRequest pageParamRequest) {
        PageMethod.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return productsMapper.queryList(queryProductsDTO);
    }

    @Override
    public CommonResult<String> addProducts(AddProductsDTO addProductsDTO) {
        return this.save(addProductsDTO)? CommonResult.success("商品新增成功") : CommonResult.failed("商品新增失败");
    }

    @Override
    public CommonResult<String> updateProducts(UpdateProductsDTO updateProductsDTO) {
        Products products = ObjectUtil.objectCopyProperties(updateProductsDTO, Products.class);
        return this.updateById(products) ? CommonResult.success("商品修改成功") : CommonResult.failed("商品修改失败");
    }
}
